<?php

	class AKB_FEATUREDQUESTIONS_PANEL  extends AKB_PANEL
	{
		/**
		* SetPanelSettings
		* This function is a chance to set any last minute global variables for
		* the template to use
		*
		* @return void
		*/
		function SetPanelSettings()
		{
			// Parse the panel of tokens, etc
			$GLOBALS['FeaturedQuestionList'] = $this->GetFeaturedQuestionList();
			$GLOBALS['Link'] = GetUrl('rss', '', 't=featured');
			if ((empty($GLOBALS['frontendUser']) || empty($GLOBALS['frontendPass']))) {
				$GLOBALS['RssButton'] = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('RssLink');
			} else {
				$GLOBALS['RssButton'] = '';
			}
		}

		/**
		* GetFeaturedQuestionList
		* Get a list of featured questions and generate the html for displaying
		* the list to the user
		*
		* @return string The html for the list
		*/
		function GetFeaturedQuestionList()
		{
			$categoryRestrictSQL = "";
			if (isset($GLOBALS['categoryRestrictOption']) && $GLOBALS['categoryRestrictOption'] == "1") {
				$accessible_cats = $GLOBALS['AKB_CLASS_HELPER']->GetPermittedCats();
				$categoryRestrict = implode(", ", $accessible_cats);
				if ($categoryRestrict == '') {
					$categoryRestrict = "''";
				}
				$categoryRestrictSQL = "AND c.categoryid IN (".$categoryRestrict.")";
			}
			
			if ((isset($GLOBALS['featuredQuestionNumber'])) && ((int)$GLOBALS['featuredQuestionNumber'] > 0)) {
				$featuredQuestionNumber = (int)$GLOBALS['featuredQuestionNumber'];
			} else {
				$featuredQuestionNumber = DEFAULT_FEATURED;
			}
			
			// Get a list of Featured questions sorted by score
			$workflowQry = '';
			if (validUrl()) {
				$workflowQry = 'AND (q.workflowstatus = \''.WF_STATUS_APPROVED.'\' OR q.workflowstatus = \''.WF_STATUS_AUTO_APPROVED.'\' OR q.workflowstatus = \''.WF_STATUS_REPENDING.'\' '.getShowDisapprovedArticleString('OR', 'q.').') ';
			}
			$query = '
				SELECT DISTINCT q.questionid, title, score, workflowstatus, q.sortorder, q.lastupdated
				FROM 
					'.$GLOBALS['tablePrefix'].'categoryassociations a, '.$GLOBALS['tablePrefix'].'categories c, 
					('.$GLOBALS['tablePrefix'].'question_auth qa RIGHT JOIN '.$GLOBALS['tablePrefix'].'questions q ON qa.questionid = q.questionid)
			
				WHERE q.questionid = a.questionid
				AND a.categoryid = c.categoryid
				AND visible=1
				'.$workflowQry.'
				AND featured=1
				AND 
					(
					((q.startdate < \''.$GLOBALS['currDatetime'].'\') AND ((q.enableexpiry = 1) AND q.expirydate > \''.$GLOBALS['currDatetime'].'\'))
					OR
					((q.startdate < \''.$GLOBALS['currDatetime'].'\') AND (q.enableexpiry = 0))
					)
				'.$categoryRestrictSQL.'
				AND (qa.groupid is null '.AKB_AUTH::deriveGroupSQL(false).') 
				ORDER BY q.sortorder DESC, q.lastupdated DESC
			'.$GLOBALS['AKB_DB']->AddLimit(0, $featuredQuestionNumber);

			$result = $GLOBALS['AKB_DB']->Query($query);

			if ($GLOBALS['AKB_DB']->CountResult($query) > 0) {
				
				$tmpRow = array();
				$historyQuestionIds = array();
				$validStatus = getShowDisapprovedArticleArray();
				while ($row = $GLOBALS['AKB_DB']->Fetch($result)) {
					if (validUrl()) {
						if (!in_array($row['workflowstatus'], $validStatus)) {
							$tmpRow[] = $row;
						} else {
							$row['title'] = '';
							$tmpRow[] = $row;
							$historyQuestionIds[] = trim($row['questionid']);
						}
					} else {
						$tmpRow[] = $row;
					}
				}
				
				if (sizeof($historyQuestionIds)) {
					$historyQuestionIdsStr = "'" . implode("','", $historyQuestionIds) . "'";
					$historyQuestionIdsQry = "SELECT  DISTINCT qh.questionid, qh.title, qh.lastupdated, qh.workflowstatus 
					FROM {$GLOBALS['tablePrefix']}questions_history qh, {$GLOBALS['tablePrefix']}catassoc_history c , {$GLOBALS['tablePrefix']}categoryassociations ca 
					WHERE (qh.workflowstatus = '".WF_STATUS_AUTO_APPROVED."' OR qh.workflowstatus = '".WF_STATUS_APPROVED."')
					AND qh.questionid = c.questionid 
					AND c.questionid = ca.questionid 
					AND c.categoryid = ca.categoryid 
					AND qh.questionid IN ($historyQuestionIdsStr)
					$categoryRestrictSQL
					ORDER BY qh.lastupdated ASC
					";
					$historyQuestionIdsQryRes = $GLOBALS['AKB_DB']->Query($historyQuestionIdsQry);
					$historyQuestion = array();
					if ($GLOBALS['AKB_DB']->CountResult($historyQuestionIdsQry) > 0) {
						while ($row = $GLOBALS['AKB_DB']->Fetch($historyQuestionIdsQryRes)) {
							$historyQuestion[$row['questionid']] = $row;
						}
					}
				}
				
				$output = $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('FeaturedQuestionsListHeader');
				foreach ($tmpRow as $row) {
					if (isset($historyQuestion[$row['questionid']])) {
						$row['title'] = $historyQuestion[$row['questionid']]['title'];
						$row['lastupdated'] = $historyQuestion[$row['questionid']]['lastupdated'];
						$row['workflowstatus'] = $historyQuestion[$row['questionid']]['workflowstatus'];
					}
					if ($row['title'] != '') {
						$link = GetUrl('question', $row['questionid']);
						if ($GLOBALS['cleanUrls']) {
							$link .= $GLOBALS['AKB_CLASS_HELPER']->_MakeURLSafe($row['title']);
						}
						$GLOBALS['Link'] = $link;
						$GLOBALS['Text'] = htmlspecialchars($GLOBALS['AKB_CLASS_TEMPLATE']->DisablePlaceholders($row['title']),ENT_QUOTES,$GLOBALS['charset']);
	
						$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('FeaturedQuestionsListRow');
					}
				}

				$output .= $GLOBALS['AKB_CLASS_TEMPLATE']->GetSnippet('FeaturedQuestionsListFooter');
			} else {
				$output = GetLang('hpNoFeaturedQuestions');
			}

			return $output;
		}
	}
?>
